package dao;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;
import utils.JdbcUtils;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

public abstract class BaseDao<T> {

    private QueryRunner runner = new QueryRunner();

    private Class<T> clazz = null;


    {
        //获取当前BaseDAO的子类继承的父类中的泛型
        Type genericSuperclass = this.getClass().getGenericSuperclass();
        ParameterizedType paramType = (ParameterizedType) genericSuperclass;

        Type[] typeArguments = paramType.getActualTypeArguments();//获取了父类的泛型参数
        clazz = (Class<T>) typeArguments[0];//泛型的第一个参数

    }


    public int update(String sql,Object...params){
        Connection conn = null;
        try {
            conn = JdbcUtils.getConnection();
            return runner.update(conn,sql,params);
        } catch (SQLException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

//    public T queryForOne(Connection conn,String sql,Object...params){
//
//        try {
//            return  runner.query(conn,sql,new BeanHandler<T>(clazz),params);
//        } catch (SQLException e) {
//            e.printStackTrace();
//        }
//        return null;
//    }

    public T queryForOne(String sql,Object...params){
        Connection conn = null;
        try {
            conn = JdbcUtils.getConnection();
            T query = runner.query(conn, sql, new BeanHandler<T>(clazz), params);
            System.out.println(query);
            return query;
        } catch (SQLException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }

    }

//    public Object queryForValue(Connection conn,String sql,Object...params){
//        try {
//            return runner.query(conn,sql,new ScalarHandler(),params);
//        } catch (SQLException e) {
//            e.printStackTrace();
//        }
//        return null;
//    }

    public Object queryForValue(String sql,Object...params){
        Connection conn = null;
        try {
            conn = JdbcUtils.getConnection();
            return runner.query(conn,sql,new ScalarHandler(),params);
        } catch (SQLException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }


//    public List<T> query(Connection conn, String sql, Object...params){
//
//        try {
//            return  runner.query(conn,sql, new BeanListHandler<>(clazz),params);
//        } catch (SQLException e) {
//            e.printStackTrace();
//        }
//        return null;
//    }

    public List<T> query(String sql, Object...params){
        Connection conn = null;
        try {
            conn = JdbcUtils.getConnection();
            return  runner.query(conn,sql, new BeanListHandler<>(clazz),params);
        } catch (SQLException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }
}
